package com.styytech.dadabao.base;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.res.Resources;
import android.graphics.drawable.AnimationDrawable;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.styytech.dadabao.R;
import com.styytech.dadabao.utils.NetworkUtils;

/**
 *
 */
public abstract class BaseActivity extends FragmentActivity {
    protected Resources resources;
    public Context context;
    /**
     * 网络信息
     */
    private TextView tv_netinfo;
    private TextView tv_topBar_Title;
    protected LinearLayout ly_left;
    protected LinearLayout ly_right;
    protected ImageButton ibtn_left;
    protected ImageButton ibtn_right;
    protected TextView tv_cancel;
    protected TextView tv_ok;
    /**
     * 加载框
     */
    protected LinearLayout ll_loginInformation;
    /**
     * 加载中图标
     */
    protected ImageView pb_loadProgressBar;
    /**
     * 提示语
     */
    protected TextView tv_infor;
    /**
     * 重试
     */
    protected ImageView iv_reloadBtn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        baseInitData();
        baseInitView();
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
      /*  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)  {
            ImmerseHelper.setSystemBarTransparent(this);
        }*/
    }

    /**
     * 初始化数据
     */
    private void baseInitData() {
        resources = getResources();
        this.context = this;
        registerReceiver();
        ViewUtils.inject(this);
    }

    /**
     * 初始化组件
     */

    private void baseInitView() {
        //重新加载
        ll_loginInformation = (LinearLayout) findViewById(R.id.ll_loginInformation);
        pb_loadProgressBar = (ImageView) findViewById(R.id.pb_loadProgressBar);
        tv_infor = (TextView) findViewById(R.id.tv_infor);
        iv_reloadBtn = (ImageView) findViewById(R.id.iv_reloadBtn);
        if (iv_reloadBtn != null) {
            iv_reloadBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    reloadBtnOnclick();
                }
            });
        }

        if (pb_loadProgressBar != null) {
            AnimationDrawable anim = (AnimationDrawable) pb_loadProgressBar
                    .getBackground();
            if (anim != null) {
                anim.start();
            }
        }
        //顶部
        ly_left = (LinearLayout) findViewById(R.id.ly_left);
        if (ly_left != null) {
            ly_left.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    baseBackClick();
                }
            });
        }
        ibtn_left = (ImageButton) findViewById(R.id.ibtn_left);
        tv_cancel = (TextView) findViewById(R.id.tv_cancel);
        tv_netinfo = (TextView) findViewById(R.id.tv_netinfo);
        tv_topBar_Title = (TextView) findViewById(R.id.tv_topBar_Title);
        if (tv_netinfo != null) {
            /**
             * 打开网络设置
             */
            tv_netinfo.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    NetworkUtils.openNetSet(context);
                }
            });
        }

        ly_right = (LinearLayout) findViewById(R.id.ly_right);
        ibtn_right = (ImageButton) findViewById(R.id.ibtn_right);
        tv_ok = (TextView) findViewById(R.id.tv_ok);
    }

    /**
     * 返回按钮处理方法，默认执行finish()方法;
     * 如需其他操作，需重写
     */
    protected void baseBackClick() {
        finish();
    }

    protected void reloadBtnOnclick() {
        //重新加载
    }

    /**
     * 设置提示信息
     *
     * @param visiLinearLayout 总体布局是否可见
     * @param visiProgressBar  加载框是否可见
     * @param visiTextView     提示信息组件
     * @param info             提示信息
     */
    public void setView(int visiLinearLayout, int visiProgressBar,
                        int visiTextView, int visiImageButn, String info) {
        if (ll_loginInformation != null)
            ll_loginInformation.setVisibility(visiLinearLayout);
        if (pb_loadProgressBar != null)
            pb_loadProgressBar.setVisibility(visiProgressBar);
        if (tv_infor != null)
            tv_infor.setVisibility(visiTextView);
        if (iv_reloadBtn != null)
            iv_reloadBtn.setVisibility(visiImageButn);
        if (tv_infor != null && info != null) {
            tv_infor.setText(info);
        }
    }

    //设置顶部栏标题
    protected void setTopBar(String title) {
        if (tv_topBar_Title != null && title != null)
            tv_topBar_Title.setText(title);
    }

    //设置顶部栏标题和左侧布局是否呈现
    protected void setTopBar(String title, int isLeft) {
        if (tv_topBar_Title != null && title != null)
            tv_topBar_Title.setText(title);
        if (ly_left != null)
            ly_left.setVisibility(isLeft);
    }

    //设置顶部栏标题、左侧布局是否呈现、左侧image是否呈现、左侧text是否呈现
    protected void setTopBar(String title, int isLeft, int isImage, String text) {
        if (tv_topBar_Title != null && title != null)
            tv_topBar_Title.setText(title);
        if (ly_left != null)
            ly_left.setVisibility(isLeft);
        if (ibtn_left != null)
            ibtn_left.setVisibility(isImage);
        if (tv_cancel != null && text != null && !text.equals("")) {
            tv_cancel.setText(text);
        }

    }

    /***
     * 注册网络广播
     */
    private void registerReceiver() {
        IntentFilter mFilter = new IntentFilter();
        mFilter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
        registerReceiver(mReceiver, mFilter);
    }

    /**
     * 网络状态监测
     */
    private BroadcastReceiver mReceiver = new BroadcastReceiver() {

        @Override
        public void onReceive(Context context, Intent intent) {
            ConnectivityManager connectivityManager;
            NetworkInfo info = null;
            String action = intent.getAction();
            if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                connectivityManager = (ConnectivityManager)

                        getSystemService(Context.CONNECTIVITY_SERVICE);
                info = connectivityManager.getActiveNetworkInfo();
                if (info != null && info.isAvailable()) {
                    String name = info.getTypeName();
                    if (tv_netinfo != null) {
                        tv_netinfo.setVisibility(View.GONE);
                    }
                } else {
                    if (tv_netinfo != null) {
                        tv_netinfo.setVisibility(View.VISIBLE);
                        tv_netinfo.setText("网络异常，请检查网络连接");
                    }
                }
            }
        }
    };

    /**
     * 隐藏键盘
     */

    public void hideSoftInput() {
        InputMethodManager manager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (getCurrentFocus() != null
                && getCurrentFocus().getWindowToken() != null) {
            manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    @Override
    protected void onDestroy() {
        hideSoftInput();
        unregisterReceiver(mReceiver);
        super.onDestroy();
    }
}
